/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gui.picker;

import java.awt.event.ActionListener;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;

/**
 * Offers a panel with two sliders to select a time
 * 
 * @author Pascal
 */
public class TimePicker extends javax.swing.JPanel {

    private ActionListener actionListener;
    private Calendar date;
    private DateFormat formatter;
    
    /**
     * Creates new form timePicker
     */
    public TimePicker(Calendar date) {
        formatter = new SimpleDateFormat("HH:mm");
        
        initComponents();
        
        setDate(date);
    }
    
    private void dateChanged(boolean setSlider) {
        labelSelectedTime.setText(formatter.format(date.getTime()));
        
        if (setSlider) {
            sliderCurrHours.setValue(date.get(Calendar.HOUR_OF_DAY));
            sliderCurrMins.setValue(date.get(Calendar.MINUTE));
        }
    }
    
    public TimePicker() {
        this(Calendar.getInstance());
    }
    
    public void setDate(Calendar date) {
        if (date == null) {
            throw new NullPointerException();
        }
        
        this.date = date;
        dateChanged(true);
    }
    
    public Calendar getDate() {
        return date;
    }
    
    public void setActionListener(ActionListener actionListener) {
        this.actionListener = actionListener;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        jLabel1 = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        jLabel2 = new javax.swing.JLabel();
        labelSelectedTime = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        sliderCurrHours = new javax.swing.JSlider();
        jLabel5 = new javax.swing.JLabel();
        sliderCurrMins = new javax.swing.JSlider();
        jSeparator2 = new javax.swing.JSeparator();
        nowButton = new javax.swing.JButton();
        okButton = new javax.swing.JButton();

        setLayout(new java.awt.GridBagLayout());

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("gui/guilabels"); // NOI18N
        jLabel1.setText(bundle.getString("choosetime")); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.insets = new java.awt.Insets(5, 0, 5, 0);
        add(jLabel1, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.insets = new java.awt.Insets(0, 7, 6, 7);
        add(jSeparator1, gridBagConstraints);

        jLabel2.setText(bundle.getString("time")); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridy = 2;
        gridBagConstraints.insets = new java.awt.Insets(7, 0, 7, 0);
        add(jLabel2, gridBagConstraints);

        labelSelectedTime.setText("14:25");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.insets = new java.awt.Insets(7, 0, 7, 0);
        add(labelSelectedTime, gridBagConstraints);

        jLabel4.setText(bundle.getString("hours")); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridy = 3;
        gridBagConstraints.insets = new java.awt.Insets(2, 0, 2, 0);
        add(jLabel4, gridBagConstraints);

        sliderCurrHours.setMaximum(23);
        sliderCurrHours.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                sliderCurrHoursStateChanged(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(2, 0, 2, 0);
        add(sliderCurrHours, gridBagConstraints);

        jLabel5.setText(bundle.getString("minutes")); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridy = 4;
        gridBagConstraints.insets = new java.awt.Insets(2, 0, 2, 0);
        add(jLabel5, gridBagConstraints);

        sliderCurrMins.setMaximum(59);
        sliderCurrMins.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                sliderCurrMinsStateChanged(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(2, 0, 2, 0);
        add(sliderCurrMins, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 5;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.insets = new java.awt.Insets(0, 6, 0, 6);
        add(jSeparator2, gridBagConstraints);

        nowButton.setText(bundle.getString("now")); // NOI18N
        nowButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                nowButtonActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridy = 6;
        gridBagConstraints.insets = new java.awt.Insets(5, 6, 5, 6);
        add(nowButton, gridBagConstraints);

        okButton.setText(bundle.getString("ok")); // NOI18N
        okButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                okButtonActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 6;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_END;
        gridBagConstraints.insets = new java.awt.Insets(5, 6, 5, 6);
        add(okButton, gridBagConstraints);
    }// </editor-fold>//GEN-END:initComponents

    private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okButtonActionPerformed
        if (actionListener != null) {
            actionListener.actionPerformed(evt);
        }
    }//GEN-LAST:event_okButtonActionPerformed

    private void sliderCurrHoursStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_sliderCurrHoursStateChanged
        date.set(Calendar.HOUR_OF_DAY, sliderCurrHours.getValue());
        dateChanged(false);
    }//GEN-LAST:event_sliderCurrHoursStateChanged

    private void sliderCurrMinsStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_sliderCurrMinsStateChanged
        date.set(Calendar.MINUTE, sliderCurrMins.getValue());
        dateChanged(false);
    }//GEN-LAST:event_sliderCurrMinsStateChanged

    private void nowButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_nowButtonActionPerformed
        setDate(Calendar.getInstance());
    }//GEN-LAST:event_nowButtonActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JLabel labelSelectedTime;
    private javax.swing.JButton nowButton;
    private javax.swing.JButton okButton;
    private javax.swing.JSlider sliderCurrHours;
    private javax.swing.JSlider sliderCurrMins;
    // End of variables declaration//GEN-END:variables
}
